<template>
  <div class="patient-create">
	  <core @on-value-change="updateFormValue" :clinic-number="clinicNumber" ref="core"></core>
	  <form-footer @on-submit="submit" @on-cancel="cancel" class="footer"></form-footer>
  </div>
</template>

<script>
import core from "./core";
import footer from "../../components/form/footer";
import { createUser } from "../../utils/actions";
export default {
	name: "patient-create",
	props: {
		clinicNumber: [Number, String]
	},
	components: {
		core,
		[footer.name]: footer
	},
	data() {
		return {
			patientInfoValue: ""
		};
	},
	methods: {
		updateFormValue(patientInfoValue) {
			this.patientInfoValue = patientInfoValue;
		},
		submit(next) {
			this.$refs.core
				.validate()
				.then(_ => {
					//提交
					createUser(this.patientInfoValue)
						.then(res => {
							this.$message.success("创建成功！");
							//提交住院基本信息
							next();
							res = JSON.parse(res);
							let patientId = res.id;
							if (this.clinicNumber) {
								//如果有值，则表示用户意图在创建患者后，同时创建病例
								setTimeout(() => {
									this.$router.push({
										path: `/case/create/${patientId}`
									});
								}, 1000);
							} else {
								history.go(-1);
							}
						})
						.catch(err => {
							next();
							this.$message.error(`创建失败！${err.message}`);
						});
				})
				.catch(err => {
					next();
				});
		},
		cancel() {
			history.go(-1);
		}
	},
	created() {}
};
</script>

<style lang="less">
.patient-create {
	.footer {
		text-indent: 10rem;
	}
}
</style>

